package com.duobaoyu.dby.supply.job.task.channel;

import com.duobaoyu.dby.channel.boundary.bean.order.param.LogisticQueryParam;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.supply.biz.client.boundary.order.OrderBoundaryClient;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderDeliveryBO;
import com.duobaoyu.dby.supply.biz.client.boundary.order.bean.bo.OrderInfoBO;
import com.duobaoyu.dby.supply.biz.producer.SupplyChainMQProducer;
import com.duobaoyu.dby.supply.biz.service.order.OrderService;
import com.duobaoyu.dby.supply.db.entity.order.OrderLogistics;
import com.duobaoyu.dby.supply.db.repository.order.OrderLogisticsRepository;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 物流任务
 *
 * @author fengshaoyu
 * @version 1.0.0
 * @date 2023/9/26 17:32
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ChannelLogisticsTask {

	private final OrderService orderService;
	private final SupplyChainMQProducer supplyChainMQProducer;
	private final OrderLogisticsRepository orderLogisticsRepository;
	private final OrderBoundaryClient orderBoundaryClient;

	/**
	 * 同步当前物流状态
	 */
	@XxlJob("channelLogisticsJobHandler")
	public void channelLogistics() {
		Integer page = CommonConstants.ZERO;
		Integer pageSize = CommonConstants.TEN;
		while (true) {
			List<OrderInfoBO> orderInfoList = orderService.noLogisticsOrders(page, pageSize);
			if (CollectionUtils.isEmpty(orderInfoList)) {
				break;
			}
			List<String> orderSnList = orderInfoList.stream().map(OrderInfoBO::getOrderSn).collect(Collectors.toList());
			List<LogisticQueryParam.ExpressInfo> expressInfo = orderService.getExpressInfo(orderSnList);

			List<OrderDeliveryBO> orderDelivery = orderBoundaryClient.queryLogisticInfoList(orderSnList,orderInfoList,expressInfo);

			List<OrderLogistics> orderLogisticsList = orderDelivery.stream().map(item -> item.getDeliveryInfoList().stream().map(deliveryInfo -> {
				if(StringUtils.isNotEmpty(deliveryInfo.getDeliveryCarrier()) && StringUtils.isNotEmpty(deliveryInfo.getDeliveryOrderSn())) {
					OrderLogistics orderLogistics = new OrderLogistics();
					orderLogistics.setOrderSn(item.getOrderSn());
					orderLogistics.setDeliveryOrderSn(deliveryInfo.getDeliveryOrderSn());
					orderLogistics.setDeliveryCarrier(deliveryInfo.getDeliveryCarrier());
					return orderLogistics;
				}
				return null;
			}).filter(Objects::nonNull).collect(Collectors.toList())).flatMap(Collection::stream).collect(Collectors.toList());
			orderLogisticsRepository.saveBatch(orderLogisticsList);

			orderInfoList.stream().filter(Objects::nonNull).collect(Collectors.toMap(OrderInfoBO::getSupplyTradeSn,OrderInfoBO::getServerName, (v1, v2) -> v2)).forEach(supplyChainMQProducer::orderAlterSend);
			page = page + pageSize;
		}
	}
}
